package com.shalanzi.hospital.controller;

import com.shalanzi.hospital.base.BasePath;
import com.shalanzi.hospital.pojo.User;
import com.shalanzi.hospital.service.UserService;
import com.shalanzi.hospital.utils.LoginUserUtil;
import com.shalanzi.hospital.vo.Welcome;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Dream_飞翔
 * @date 2021/9/25
 * @time 22:15
 * @email 1072876976@qq.com
 */
@Controller
public class IndexController extends BasePath {
    @Resource
    private UserService userService;

    /**
     * 跳转到用户登录
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 跳转到系统首页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        // 获取cookie中的用户Id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        // 查询用户队对象并设置Session作用域
        User user = userService.selectUserById(userId);
        // 将用户对象设置到session作用域中
        request.getSession().setAttribute("user", user);
        return "index";
    }

    /**
     * 跳转欢迎页面
     *
     * @return
     */
    @RequestMapping("/welcome")
    public String welcome(HttpServletRequest request) {
        Welcome welcome = userService.showHospitalData();
        if (welcome != null)
            request.getSession().setAttribute("hospital", welcome);
        return "welcome";
    }
}
